package cn.jeefast.service.impl;

import cn.jeefast.dao.HuankuanDao;
import cn.jeefast.entity.Huankuan;
import cn.jeefast.service.HuankuanService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yaodongyun
 * @since 2019-02-26
 */
@Service
public class HuankuanServiceImpl extends ServiceImpl<HuankuanDao, Huankuan> implements HuankuanService {
    @Resource
     HuankuanDao dao;
    @Override
    public void insertHuanKuan(Huankuan huankuan) {
        System.out.println(dao.insert(huankuan));
        System.out.println("导入成功");
    }
    @Override
    public Date yujidate(Date cunkuandate, int jiesuanzhouqi) throws Exception {
        Date yujidate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar_cunkuan = Calendar.getInstance();
        calendar_cunkuan.setTime(cunkuandate);
        int day_cunkuan = calendar_cunkuan.get(Calendar.DATE);
        int month_cunkuan = calendar_cunkuan.get(Calendar.MONTH) + 1;//获取月份
        int year_cunkuan = calendar_cunkuan.get(Calendar.YEAR);//获取年份
        int maxDay_cunkuan = calendar_cunkuan.getActualMaximum(Calendar.DAY_OF_MONTH); //当前月的最大天数
        if (jiesuanzhouqi == 30) {
            if (month_cunkuan == 2 && day_cunkuan >= 28) {   //2月份28 存款
                int mon = month_cunkuan + 1;
                int daya = 28;
                yujidate = sdf.parse(year_cunkuan + "-" + mon + "-" + 30);
            } else if (day_cunkuan >= 30) {
                if (month_cunkuan >= 12) {             //10月份以上存款， 要加一年
                    int mon = month_cunkuan + 1 - 12;
                    int year = year_cunkuan + 1;
                    calendar_cunkuan.setTime(sdf.parse(year + "-" + mon + "-" + 1));
                    int maxday = calendar_cunkuan.getActualMaximum(Calendar.DAY_OF_MONTH);
                    if (maxday < 30) {
                        yujidate = sdf.parse(year + "-" + mon + "-" + maxday);
                    } else {
                        yujidate = sdf.parse(year + "-" + mon + "-" + 30);
                    }
                } else {                        //其他月份存款， 月份+3， 天数30
                    int mon = month_cunkuan + 1;
                    yujidate = sdf.parse(year_cunkuan + "-" + mon + "-" + 30);
                }
            } else { //天数<30
                if (month_cunkuan >= 12) {             //10月份以上存款， 要加一年
                    int mon = month_cunkuan + 1 - 12;
                    int year = year_cunkuan + 1;
                    yujidate = sdf.parse(year + "-" + mon + "-" + day_cunkuan);
                } else {                        //其他月份存款， 月份+3， 天数30
                    int mon = month_cunkuan + 1;
                    yujidate = sdf.parse(year_cunkuan + "-" + mon + "-" + day_cunkuan);
                }
            }
        }
        return yujidate;
    }
}
